package com.huawei.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Customer;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.mapper.CustomerMapper;
import com.huawei.crm.query.QueryObject;
import com.huawei.crm.service.ICustomerService;
import com.huawei.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl extends AbstractService<CustomerMapper, Customer, Long> implements ICustomerService {

    @Autowired
    private CustomerMapper mapper;

    @Override
    public void saveOrUpdateC(Customer c) {
        if(c.getId() == null){
            Employee currentEmp = UserContext.getCurrentEmp();
            c.setInputUser(currentEmp);
            c.setInputTime(new Date());
            mapper.insert(c);
        }else {
            mapper.updateByPrimaryKey(c);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Customer> query(QueryObject qo) {
        // 告诉mybatis需要去哪里拼接sql
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void updateStatus(Long customerId, Integer status) {
        mapper.updateStatus(customerId, status);
    }

}
